package barbershop.com.service.statistic.impl;

import barbershop.com.dao.orders.OrderDao;
import barbershop.com.dao.orders.OrderItemDao;
import barbershop.com.entity.orders.OrderItem;
import barbershop.com.entity.orders.Orders;
import barbershop.com.service.statistic.StatisticService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class StatisticServiceImpl implements StatisticService {
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private OrderItemDao orderItemDao;

    @Override
    public List<Orders> getWeeklyOrders() {
        try {
            ArrayList<String> dateList = this.getCurrentWeekDays();
            List<Orders> orderList = this.orderDao.findOrdersByRecentWeek(dateList.get(6),dateList.get(0),1);
            System.out.println("过去七天交易成功的订单获取成功！");
            return orderList;
        }   catch (Exception e){
            e.printStackTrace();
            System.out.println("过去七天交易成功的订单获取失败！");
        }
        return null;
    }

    @Override
    public Integer getServiceTypeWeeklyStat(Integer serviceTypeId) {
        try {
            List<Orders> orderList = this.getWeeklyOrders();
            Integer sum = this.calServType(orderList, serviceTypeId);
            System.out.println("近七天编号为"+serviceTypeId+"的服务类型的消费总数是："+sum);
            return sum;
        }   catch (Exception e){
            e.printStackTrace();
            System.out.println("近七天编号为"+serviceTypeId+"的服务类型的消费总数统计失败！");
        }
        return null;
    }

    @Override
    public ArrayList<String> getCurrentWeekDays() {
        try {
            ArrayList<String> currentWeekDays = new ArrayList<>();
            for (int day=0; day<7 ; day++) {
                Calendar calendar = Calendar.getInstance();
                calendar.add(Calendar.DATE,-day);   // 日期从今天往前推
                Date today = calendar.getTime();
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                String date = format.format(today);
                currentWeekDays.add(date);
            }
            System.out.println("最近一周的日期获取成功！");
            return currentWeekDays;
        }   catch (Exception e){
            e.printStackTrace();
            System.out.println("最近一周的日期获取失败！");
        }
        return null;
    }

    @Override
    public Double getDailyTotalStat(String date) {
        try {
            List<Orders> orderList = this.orderDao.findOrdersByRecentDay(date);
            Double totalAmount = 0.0;
            for (Orders order: orderList){
                if (order.getOrderTotalPrice()!=null)
                    totalAmount += order.getOrderTotalPrice();
            }
            System.out.println(date+"的营业额"+totalAmount+"元获取成功！");
            return totalAmount;
        }   catch (Exception e){
            e.printStackTrace();
            System.out.println("营业额获取失败！");
        }
        return null;
    }

    @Override
    public List<Orders> getIntervalOrders(String startDate, String endDate) {
        try {
            List<Orders> ordersList = this.orderDao.findOrdersByRecentWeek(startDate, endDate, 1);
            System.out.println(startDate+"到"+endDate+"内的所有订单获取成功！");
            return ordersList;
        }   catch (Exception e){
            e.printStackTrace();
            System.out.println(startDate+"到"+endDate+"内的所有订单获取失败！");
        }
        return null;
    }

    @Override
    public Integer getServiceWeeklyStat(Integer serviceId) {
        try {
            Integer sum = 0;
            List<Orders> orderList = this.getWeeklyOrders();
            for (Orders order: orderList){
                // 每条订单下的条目
                List<OrderItem> orderItemList =
                        this.orderItemDao.findAllByOrdersOrderId(order.getOrderId());
                for (OrderItem orderItem: orderItemList){
                    // 对每列条目里的服务项目进行比对
                    if (orderItem.getService().getServiceId() == serviceId)
                        sum += orderItem.getQuantity();
                }
            }
            System.out.println("近七天编号为"+serviceId+"的服务项目的消费总数是："+sum);
            return sum;
        }   catch (Exception e){
            e.printStackTrace();
            System.out.println("近七天编号为"+serviceId+"的服务项目的消费总数获取失败！");
        }
        return null;

    }

    @Override
    public Integer getServiceTypeStat(Integer serviceTypeId, String startDate, String endDate) {
        try {
            List<Orders> ordersList = this.getIntervalOrders(startDate, endDate);
            Integer sum = this.calServType(ordersList, serviceTypeId);
            System.out.println(startDate+"到"+endDate+"关于id为"+serviceTypeId+"的服务类型的消费总数是："+sum);
            return sum;
        }   catch (Exception e){
            e.printStackTrace();
            System.out.println(startDate+"到"+endDate+"关于id为"+serviceTypeId+"的服务类型的消费总数获取失败！");
        }
        return null;
    }

    @Override
    public ArrayList<String> getIntervalDays(String startDate, String endDate) {
        try {
            ArrayList<String> intervalDays = new ArrayList<>();
            // 获取本段时间日期
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date dBegin = sdf.parse(startDate);
            Date dEnd = sdf.parse(endDate);
            List<Date> dateList = findDates(dBegin, dEnd);
            for (Date date: dateList){
                intervalDays.add(sdf.format(date));
            }
            System.out.println(startDate+"到"+startDate+"的日期获取成功");
            return intervalDays;
        }   catch (Exception e){
            e.printStackTrace();
            System.out.println(startDate+"到"+startDate+"的日期获取失败！");
        }
        return null;
    }

    /** 某些可复用的代码 **/
    //获取某段时间内的所有日期 
    public List<Date> findDates(Date dBegin, Date dEnd) {
        List dateList = new ArrayList();
        dateList.add(dBegin);
        Calendar calBegin = Calendar.getInstance();
        // 使用给定的 Date 设置此 Calendar 的时间
        calBegin.setTime(dBegin);
        Calendar calEnd = Calendar.getInstance();
        // 使用给定的 Date 设置此 Calendar 的时间
        calEnd.setTime(dEnd);
        // 测试此日期是否在指定日期之后
        while (dEnd.after(calBegin.getTime())) {
            // 根据日历的规则，为给定的日历字段添加或减去指定的时间量
            calBegin.add(Calendar.DAY_OF_MONTH, 1);
            dateList.add(calBegin.getTime());
        }
        return dateList;
    }
    // 抽出来的计算服务类型的代码
    public Integer calServType(List<Orders> orderList, Integer serviceTypeId){
        try {
            Integer sum = 0;
            for (Orders order: orderList){
                // 每条订单下的条目
                List<OrderItem> orderItemList =
                        this.orderItemDao.findAllByOrdersOrderId(order.getOrderId());
                for (OrderItem orderItem: orderItemList){
                    // 对每列条目里的服务类型进行比对
                    if (orderItem.getService().getServiceType().getServiceTypeId() == serviceTypeId)
                        sum += orderItem.getQuantity();
                }
            }
            return sum;
        }   catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}
